package com.apps.ftapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MyCouponAdapter;
import com.apps.ftapp.model.MyCouponModel;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;

/**
 * 我的优惠券
 */
public class MyCouponActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private Gson gson = new Gson();

    private List<MyCouponModel.CouponsBeanX.CouponsBean> dataList = new ArrayList<>();
    private MyCouponAdapter myCouponAdapter;

    private String state = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_coupon);
        ButterKnife.bind(this);

        setTitle("优惠券");

        //新页面接收数据
        Bundle bundle = this.getIntent().getExtras();
        //接收name值
        try {
            state = bundle.getString("state");
        } catch (Exception e) {
            state = null;
        }


        initView();

        loadData();

    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("my/useable_coupons", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", ">>>>>" + new String(responseBody));
                    MyCouponModel result = gson.fromJson(new String(responseBody), MyCouponModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        dataList.addAll(result.getCoupons().getCoupons());
                        myCouponAdapter.addRes(result.getCoupons().getCoupons());
                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                L.i("main", ">>>>>" + new String(responseBody));

            }
        });

    }

    private void initView() {

        myCouponAdapter = new MyCouponAdapter(getApplicationContext(), dataList);
        listView.setAdapter(myCouponAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (state != null) {
                    Intent mIntent = new Intent();
                    mIntent.putExtra("couponId", dataList.get(position).getId());
                    mIntent.putExtra("value", dataList.get(position).getValue());
                    mIntent.putExtra("title", dataList.get(position).getTitle());
//                    setResult
                    setResult(2, mIntent);
                    finish();

                    //
                }
            }
        });

    }


}
